package com.xhwl.centiotdata.pollingtrigger.patrol;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.security.SecurityIntegrationFactory;
import com.xhwl.centiotdata.pojo.dto.huawei.IOTConfigDTO;
import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.integrateconfig.SecurityIntegrationConfig;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.utils.ChkUtil;
import com.xhwl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * Falcon.cao @Copyright (C), 2022, 兴海物联
 * Date: 2022/9/15 8:28
 * Description: 包信息
 */
@Component
public class SecurityPatrolDispatcher implements ScheduleDispatcher {

    @Autowired
    private SecurityIntegrationConfig securityIntegrationConfig;

    @Override
    public void doOneHourTask() {
        List<DeviceProjSupplierCategory> configs = securityIntegrationConfig.getIntegrationConfigs();
        if (ChkUtil.isNotNull(configs)) {

            JSONObject params = new JSONObject();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            params.put("startTime", format.format(calendar.getTime()));
            calendar.add(Calendar.MINUTE, -75);
            params.put("endTime", format.format(calendar.getTime()));

            for (DeviceProjSupplierCategory config : configs) {
                JSONObject properties = config.getProperties();
                if (ChkUtil.isNull(properties)) {
                    continue;
                }
                try {

                    IOTConfigDTO configDTO = properties.toJavaObject(IOTConfigDTO.class);
                    SecurityIntegrationFactory.getInvokeStrategy(config.getProductId()).onGettingCheckRecord(configDTO, params, configDTO.getCheckRecord());
                } catch (Exception e) {
                    // Nothing need to do
                }
            }
        }
    }
}
